<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图-滚动版</title>
    <style>
        .wrapper {
            width: 600px;
            height: 400px;
            overflow: hidden;
            margin: 0 auto;
            position: relative;
        }
        .wrapper-box {
            width: auto;
            display: flex;
            transition: 500ms;
        }
        .img-box {
            width: 600px;
            height: 400px;
            overflow: hidden;
            /* 图片不压缩 */
            flex-shrink: 0;
        }
        .img-box img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        .btns {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            width: 100%;
            display: flex;
            justify-content: space-between;
            padding: 0 10px;
            box-sizing: border-box;
        }
        .btns .pre,
        .btns .next {
            cursor: pointer;
            user-select: none;
            font-size: 50px;
            transition: 300ms;
        }
        .btns .pre {
            transform: translateX(-120%);
        }
        .btns .next {
            transform: translateX(120%);
        }
        .wrapper:hover .btns span {
            transform: translateX(0);
        }
        .controls {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
        }
        .controls span {
            display: inline-block;
            width: 10px;
            height: 10px;
            background-color: #fff;
            border-radius: 50%;
            margin: 0 10px;
            cursor: pointer;
        }
        .controls span.active {
            background-color: red;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <div class="wrapper-box">
            <div class="img-box">
                <img src="./img/case20/01.png" alt="">
            </div>
        </div>
        <div class="btns">
            <span class="pre">&lt;</span>
            <span class="next">&gt;</span>
        </div>
        <div class="controls">
            
        </div>
    </div>
    <script>
        let arr = [
            "./img/case20/01.png",
            "./img/case20/02.png",
            "./img/case20/03.png",
            "./img/case20/04.png"
        ]
        const wrapper = document.querySelector(".wrapper")
        const wrapperBox = wrapper.querySelector(".wrapper-box")
        // 循环显示图片
        function renderImg(){
            wrapperBox.innerHTML = arr.map(item=>{
                return `
                <div class="img-box">
                    <img src="${item}" alt="">
                </div>
                `
            }).join("")
        }
        renderImg()

        // 保存当前显示图片的下标值
        let curIndex = 0;
        // 添加控制器
        const controls = document.querySelector(".controls")
        function addControls(){
            let allSpan = ""
            for(let i=0;i<arr.length;i++){
                allSpan = allSpan + `<span data-index="${i}" class="control"></span>`
            }
            controls.innerHTML = allSpan
            controlsLight()
        }
        addControls()

        // 设置控制器高亮
        function controlsLight(){
            let allSpan = controls.children
            allSpan = Array.from(allSpan)
            allSpan.forEach(item=>{
                item.classList.remove("active")
            })
            allSpan[curIndex].classList.add("active")
        }
        
        // 下一页
        const next = document.querySelector(".next")
        const moveW = wrapper.offsetWidth;
        next.addEventListener("click", function(){
            if(curIndex == arr.length-1){
                curIndex = 0
            }else{
                curIndex++
            }
            wrapperBox.style.transform = `translateX(-${moveW*curIndex}px)`
            controlsLight()
        })

        // 上一页
        const pre = document.querySelector(".pre")
        pre.addEventListener("click", function(){
            if(curIndex == 0){
                curIndex = arr.length-1
            }else{
                curIndex--
            }
            
            wrapperBox.style.transform = `translateX(-${moveW*curIndex}px)`
            controlsLight()
        })

        // 点击控制器
        controls.addEventListener("click", function(e){
            if(e.target.classList.contains("control")){
                curIndex = e.target.dataset.index
                wrapperBox.style.transform = `translateX(-${moveW*curIndex}px)`
                controlsLight()
            }
        })

        // 自动播放
        let timer = null
        function startInterval(){
            timer = setInterval(() => {
                next.click()
            }, 2000);
        }
        startInterval()

        // 暂停播放
        wrapper.addEventListener("mouseenter", function(){
            clearInterval(timer)
        })
        // 重新播放
        wrapper.addEventListener("mouseleave", function(){
            startInterval()
        })
    </script>
</body>
</html>